package net.pws.oos.web.dto;

import java.util.Date;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.Indicator;
import net.pws.oos.biz.model.User;
import net.pws.oos.security.SecurityUtils;

import org.apache.commons.lang.StringUtils;

public class IndicatorDto extends Indicator {
    
	private static final long serialVersionUID = -8204623801163155965L;

	public static IndicatorDto from(Indicator indicator) {
        IndicatorDto result = new IndicatorDto();
        String[] props = {"parent", "creator", "createOrganization"};
        BeanUtils.copyProperties(result, indicator, props);
        Indicator parent = indicator.getParent();
        if (null != parent) {
            result.setParentId(parent.getId());
            result.setParentName(parent.getName());
        }
        User creator = indicator.getCreator();
        if (null != creator) {
            result.setCreatorId(creator.getId());
            result.setCreatorName(creator.getName());
        }
//        Organization createOrganization = indicator.getCreateOrganization();
//        if (null != createOrganization) {
//            result.setCreateOrganId(createOrganization.getId());
//            result.setCreateOrganName(createOrganization.getName());
//        }
        return result;
    }
    
    public static Indicator to(IndicatorDto dto, EntityManager entityManager) {
        Indicator result = new Indicator();
        if (!StringUtils.isEmpty(dto.getId())) {
            result = entityManager.find(Indicator.class, dto.getId());
            
            if (null == result) {
                throw new RuntimeException("无效的指标项id");
            }
//            if (!StringUtils.isEmpty(result.getSourceSystem())) {
//                throw new RuntimeException("不能修改同步自其他系统的指标项！");
//            }
        }
//        result.setName(dto.getName());
//        result.setCode(dto.getCode());
//        result.setShortName(dto.getShortName());
////        result.setDistrictCode(dto.getDistrictCode());
////        result.setDistrictName(dto.getDistrictName());
////        result.setLongitude(dto.getLongitude());
////        result.setLatitude(dto.getLatitude());
//        result.setCode(dto.getCode());
//        result.setDescription(dto.getDescription());
//        if (!StringUtils.isEmpty(dto.getParentId())) {
//            Indicator parent = entityManager.find(Indicator.class,
//                                                     dto.getParentId());
//            result.setParent(parent);
//        }
        
        String[] ignoredProps = { "id","depth","innerCode","children","leaf",
        		"deleted","createDateTime","deleteDateTime", "inputType",
				"parent", "creator", "createOrganization"};
		BeanUtils.copyProperties(result, dto, ignoredProps);
//        result.setName(dto.getName());
//
//        result.setDescription(dto.getDescription());

        
		if (StringUtils.isNotEmpty(dto.getParentId())) {
            Indicator parent = entityManager.find(Indicator.class, dto.getParentId());
            result.setParent(parent);
        }
        
        if (StringUtils.isEmpty(dto.getCreatorId())) {        	
        	result.setCreator(entityManager.find(User.class, SecurityUtils.currentUserId()));        	
		} else {
			result.setCreator(entityManager.find(User.class, dto.getCreatorId()));
		}
        
//        if (StringUtils.isEmpty(dto.getCreateOrganId())) {
//        	result.setCreateOrganization(null);   	
//		} else {
//			result.setCreateOrganization(entityManager.find(Organization.class, dto.getCreateOrganId()));
//		}
//        
        if (result.getCreateOrganization() == null && result.getCreator() != null) {
        	result.setCreateOrganization(result.getCreator().getOrganization());
		}

        if (result.getCreateDateTime() == null) {
        	result.setCreateDateTime(new Date());
        }
        return result;
    }
    
    private String parentId;
    
    private String parentName;
    
    private String creatorId;
    
    private String creatorName;
    
    private String createOrganId;
    
    private String createOrganName;
    
    public String getParentId() {
        return parentId;
    }
    
    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
    
    public String getParentName() {
        return parentName;
    }
    
    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

	public String getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public String getCreateOrganId() {
		return createOrganId;
	}

	public void setCreateOrganId(String createOrganId) {
		this.createOrganId = createOrganId;
	}

	public String getCreateOrganName() {
		return createOrganName;
	}

	public void setCreateOrganName(String createOrganName) {
		this.createOrganName = createOrganName;
	}    
    
}
